package cn.wolfcode.crud.web.controller;

import cn.wolfcode.crud.domain.Department;
import cn.wolfcode.crud.qo.JsonResult;
import cn.wolfcode.crud.qo.PageResult;
import cn.wolfcode.crud.qo.QuerObjcet;
import cn.wolfcode.crud.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    protected IDepartmentService adepartment;

    //查询操作
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QuerObjcet qo) {
        model.addAttribute("PageResult", adepartment.query(qo));
        return "department/list";
    }

    //删除操作
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        try {
            if (id != null) {
                adepartment.delete(id);
            }
            return new JsonResult() ;
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"操作失败") ;
        }


    }

    //    //跳转页面
//    @RequestMapping("/input")
//    public String input(Long id,Model model){
//        if (id !=null){
//            Department department = adepartment.get(id);
//            model.addAttribute("department",department);
//        }
//        return "/department/input";
//    }
    //添加和修改
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department department) {
        try {
            if (department.getId() != null) {
                adepartment.update(department);
            } else {
                adepartment.insert(department);
            }
             return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败啦");

        }
    }



}
